package com.langsin.my0628;


import com.langsin.bean.User;
import com.langsin.jiangjinheng.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;

public class JiangJinHengTest {
    //二级缓存
    @Test
    public void testCache2() throws Exception{
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = builder.build(stream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper mapper1 = sqlSession.getMapper(UserMapper.class);
        User user1 = mapper1.queryById(702);
        System.out.println(user1);
        sqlSession.close();

        try {
            Thread.sleep(2000);}catch (Exception e){e.printStackTrace();
        }

        SqlSession sqlSession2 = sessionFactory.openSession();
        UserMapper mapper2 = sqlSession2.getMapper(UserMapper.class);
        User user2 = mapper2.queryById(702);
        System.out.println(user2);
        sqlSession.close();
    }
    //一级缓存
    @Test
    public void testCache1() throws Exception{
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sessionFactory = builder.build(stream);
        SqlSession sqlSession = sessionFactory.openSession();

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryById(702);
        System.out.println(user);

        try {
            Thread.sleep(2000);}catch (Exception e){e.printStackTrace();
        }

        User user1 = userMapper.queryById(702);
        System.out.println(user1);
    }
}
